package com.company.test35_01;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by v on 16-12-12.
 */
public class CircleView extends JPanel implements ActionListener {
    private CircleModel model;

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
    }

    public void setModel(CircleModel model) {
        this.model = model;

        if (model != null)
            model.addActionListener(this);

        repaint();
    }

    public CircleModel getModel() {
        return model;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (model == null) return;

        g.setColor(model.getColor());

        int xCenter = getWidth() / 2;
        int yCenter = getHeight() / 2;
        int radius = (int) model.getRadius();

        if (model.isFilled()) {
            g.fillOval(xCenter - radius, yCenter - radius,
                    2 * radius, 2 * radius);
        } else {
            g.drawOval(xCenter - radius, yCenter - radius,
                    2 * radius, 2 * radius);
        }
    }
}